﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.Lib;
using Com.Seezt.Skins;

namespace UI.CommonForm
{
    public partial class FrmLoading : BasicForm
    {
        protected string ErrMsg = "";
        delegate void SetTextCallback(string msg, bool isError);
        int stepCount = 7;
        public int count = 1;
        public DateTime startTime = DateTime.Now;

        public FrmLoading()
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            this.lblStatus.Text = "";
            this.prgMain.Maximum = stepCount;
            this.prgMain.Value = 0;
            startTime = DateTime.Now;
        }

        public void SetPlan(string msg, bool isError)
        {
            // InvokeRequired需要比较调用线程ID和创建线程ID
            // 如果它们不相同则返回true
            if (!this.lblStatus.InvokeRequired)
            {
                try
                {
                    SetTextCallback d = new SetTextCallback(SetPlan);
                    this.BeginInvoke(d, new object[] { msg, isError });
                }
                catch { }
            }
            else
            {
                this.lblStatus.Text = "";
                this.lblStatus.Update();
                this.Update();

                this.lblStatus.Text = msg;
                this.lblStatus.Update();

                Increase();

                if (DateTime.Now.Subtract(startTime).Seconds > 6)
                {
                    ErrMsg = (new PsException(4)).ErrMsg;
                }

                if (isError)
                    ErrMsg = msg;
            }
        }

        //显示错误
        public void SetErrorMsg()
        {
            this.lblStatus.ForeColor = Color.Red;
            this.lblStatus.Font = new Font("宋体", 12, FontStyle.Bold);

            this.lblStatus.Text = Msg;
            this.lblStatus.Update();
        }

        public string Msg
        {
            get { return ErrMsg; }
        }

        public void SetText1()
        {
            if (!this.lblStatus.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetPlan);
                this.BeginInvoke(d);
            }
            else
            {
                this.lblStatus.Text = "";
                this.lblStatus.Update();
                this.Update();

                this.lblStatus.Text = "正在提取验证消息……";
                Increase();
                this.lblStatus.Update();
            }
        }

        public void SetText2()
        {
            if (!this.lblStatus.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetPlan);
                this.BeginInvoke(d);
            }
            else
            {
                this.lblStatus.Text = "";
                this.lblStatus.Update();
                Increase();
                this.Update();

                this.lblStatus.Text = "登录成功，准备加载菜单……";
                this.lblStatus.Update();
            }
        }

        /// <summary>
        /// Increase process bar
        /// </summary>
        /// <param name="nValue">the value increased</param>
        /// <returns></returns>
        public void Increase()
        {
            if (count > this.prgMain.Maximum) return;
            this.prgMain.Value = count++;
        }
    }
}
